package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.CityInfoService;
import com.atguigu.lease.admin.service.DistrictInfoService;
import com.atguigu.lease.admin.service.ProvinceInfoService;
import com.atguigu.lease.pojo.CityInfo;
import com.atguigu.lease.pojo.DistrictInfo;
import com.atguigu.lease.pojo.ProvinceInfo;
import com.atguigu.lease.result.Result;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/admin/region")
@RestController
public class RegionController {
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    @GetMapping("/province/list")
    public Result<List<ProvinceInfo>> listProvince() {
        List<ProvinceInfo> list = provinceInfoService.list();
        return Result.success(list);
    }

    @GetMapping("/city/listByProvinceId")
    public Result<List<CityInfo>> listCity(@RequestParam("id") Long id) {
        List<CityInfo> list = cityInfoService.list(Wrappers.lambdaQuery(CityInfo.class).eq(CityInfo::getProvinceId,id));
        return Result.success(list);
    }

    @GetMapping("/district/listByCityId")
    private Result<List<DistrictInfo>> listDistrict(@RequestParam("id") Long id) {
        List<DistrictInfo> list = districtInfoService.list(Wrappers.lambdaQuery(DistrictInfo.class).eq(DistrictInfo::getCityId, id));
        return Result.success(list);
    }
}
